////<!--数据推送-->
import api from '../../../fetch/api'

export default {
    data() {
        return {
            bigImgPath: '',
            id: '',
            qrImg:'',            
            reports: [],//可选的报表
            reportsSelected: [],
            reportsDisabled:[],//禁用的报表
            reportsDisabledSelected:[],            
            weixinAccounts: [],
            weixinRadio: '',
            weixinReports:[],//已选定账户的关系
            dataPushReportsList:[],//点击微信用户“查看”的报表列表
            dataPushReportsListSelected:[],
            radio:[],
            weixinedList:[],//点击的微信用户
        }
    },
    created() {
    },
    watch: {
       "weixinRadio": function (val) {
          let _this = this;
          _this.reportsSelected=[];
          for(let i=0;i<_this.weixinReports.length;i++){
            if(_this.weixinReports[i].id==val){
                if(_this.weixinReports[i].dataPushReportsList.length==0){
                    _this.reportsSelected=[];
                    return;
                }                
                for(let j=0;j<_this.weixinReports[i].dataPushReportsList.length;j++){
                    _this.reportsSelected.push(_this.weixinReports[i].dataPushReportsList[j].id);
                }
            }
          }
        },
    },
    methods: {
        getList() {
            let params = $.extend({}, this.searchParams, this.otherParams);
            this.getItemList(params);
        },
        showBigImgModal(name, path) {
            this.bigImgPath = path;
            this.toOpenImgModal(name, path);
        },
        //获取列表
        getItemList(aparams) {
            let self = this,
                params = {
                    page: this.pageNumber,
                    rp: this.pageSize
                };
            params = $.extend({}, params, aparams);
            api.fetchGet('/report/dataPush/pageList.do', params)
                .then(res => {
                    let data = res.data,
                        list = data.rows;
                    self.tbodysInfo = list;
                    self.total = data.total;
                }, err => { })
        },
        exportData() {
            let self = this;
            let params = {
            };
            params = $.extend({}, params, this.searchParams, this.otherParams);
            self.showDialog("确认导出该报表?", '', function () {
                self.toExport('report/dataPush/export.do', params)
            });
        },
        addRemark(modalName, id) {
            let self = this;
            self.id = id;
            console.log(self.id);
            console.log(self.tbodysInfo[0]);
            for(let i=0;i<self.tbodysInfo.length;i++){
                if(self.tbodysInfo[i].id==self.id){
                    self.remark.content=self.tbodysInfo[i].remark;
                }
            }
            this.toOpenModal(modalName);
        },
        //添加备注
        submitTypeForm(formName) {
            let self = this;
            let params = {
                id: self.id,
                remark: self.remark.content
            };
            params = $.extend({}, params);

            self.$refs[formName].validate((valid) => {
                if (valid) {
                    self.showDialog("确认更新备注信息?", '', function () {
                        api.fetchPost('/report/dataPush/update.do', params)
                            .then(res => {
                                //start
                                if (res.status == 1) {
                                    self.showToast(res.message, 1, 2000);
                                    //初始化搜索列表
                                    self.getList();
                                    self.closed('remarkModal', 1);
                                    return;
                                } else {
                                    self.showToast(res.message, 2, 2000)
                                    return;
                                }
                                //end
                            }, err => { });
                    });
                } else {
                    return false;
                }
            });
        },
        //删除数据推送条目
        deleteDataPush(id) {
            let self = this;
            let params = {
                id: id
            };
            params = $.extend({}, params);
            self.showDialog("确认删除该推送人员及数据推送配置信息?", '', function () {
                api.fetchPost('/report/dataPush/del.do', params)
                    .then(res => {
                        //start
                        if (res.status == 1) {
                            self.showToast(res.message, 1, 2000);
                            //初始化搜索列表
                            self.getList();
                            self.closed('remarkModal', 1);
                            return;
                        } else {
                            self.showToast(res.message, 2, 2000)
                            return;
                        }
                        //end
                    }, err => { });
            });
        },
        generateQr() {
            this.toOpenModal("qrModal");
            // let uri = window.location.href.split("/#")[0] + "/mgr/report/dataPush/generateQr.do"+"?"+((new Date()).getTime());
            let uri = window.location.protocol + '//' + window.location.host + "/mgr/report/dataPush/generateQr.do"+"?"+((new Date()).getTime());
            console.log(uri);
            this.qrImg=uri;//'http://127.0.0.1:5002/mgr/report/dataPush/generateQr.do';
        },
        //得到用户微信号条目
        getUserWeixins() {
            let self = this;
            let params = {
            };
            params = $.extend({}, params);
            api.fetchGet('/report/dataPush/userList.do', params)
                .then(res => {
                    let data = res.data,
                        list = data.rows;
                    for(let i=0;i<list.length;i++){
                        self.weixinAccounts.push(list[i]);
                        if(i==0){//初始化已选
                            self.weixinRadio=list[i]; 
                        }
                    }
                    console.log("weixinAccounts:"+self.weixinAccounts);
                    if(self.weixinAccounts.length==0){
                        self.showToast("没有需要绑定的用户", 2, 2000)
                        return;
                    }
                    self.getReports();
                    self.toOpenModal("bindModal"); 
            }, err => { });
        },
        //得到报表条目
        getReports() {
            let self = this;
            let params = {
            };
            params = $.extend({}, params);
            api.fetchGet('/report/dataPush/reportList.do', params)
                .then(res => {
                    let data = res.data,
                        list = data.rows;
                    for(let i=0;i<list.length;i++){
                        if(list[i].actived==1){
                            self.reports.push(list[i]);
                        }else{
                            self.reportsDisabled.push(list[i]);
                        }
                        
                    }
            }, err => { });
        },
        getUserReports() {
            let self = this;
            let params = {
            };
            params = $.extend({}, params);
            api.fetchGet('/report/dataPush/userReportList.do', params)
                .then(res => {
                    let data = res.data,
                        list = data.rows;
                    for(let i=0;i<list.length;i++){
                        self.weixinReports.push(list[i]);
                    }
            }, err => { });
        },
        bindPushContent() {
            this.$router.push({path:'/report/push_report'})
            return;
            let self = this;
            self.weixinAccounts=[];
            self.reports=[];
            self.reportsSelected=[];
            self.reportsDisabled=[];
            self.reportsDisabledSelected=[]; 
            self.weixinRadio='';
            self.weixinReports=[];
            self.getReports();
            self.getUserReports();            
            self.toOpenModal("bindModal");                       
        },
        submitBind() {
            let self = this;
            let params = {
                userId: self.weixinRadio,
                reportIds:'',
            };
            let x=[];
            for(let i=0;i<self.reportsSelected.length;i++){
                x.push(self.reportsSelected[i]);
            }
            if(self.weixinRadio==null){
                self.showToast("请选择推送人员", 2, 2000);
                return;
            }
            if(x.length==0){
                self.showToast("请选择推送报表", 2, 2000);
                return;
            }
            params.reportIds=JSON.stringify(x);

            params = $.extend({}, params);
            self.showDialog("确认进行数据绑定吗?", '', function () {
                api.fetchPost('/report/dataPush/bind.do', params)
                    .then(res => {
                        //start
                        if (res.status == 1) {
                            self.showToast(res.message, 1, 2000);
                            self.closed('bindModal', 1);
                            self.getList();
                            return;
                        } else {
                            self.showToast(res.message, 2, 2000)
                            return;
                        }
                        //end
                    }, err => { });
            });
            
        },
        showModal(row) {
            var self=this;
            self.dataPushReportsList=[];
            self.dataPushReportsListSelected=[];
            self.radio=[];
            self.weixinedList=[];
            self.dataPushReportsList = row.dataPushReportsList;
            for(let i=0;i<self.dataPushReportsList.length;i++){
                self.dataPushReportsListSelected.push(self.dataPushReportsList[i].id);
            }
            self.weixinedList.push(row.weixinId);
            self.radio.push(row.weixinId);
            self.toOpenModal('bindedModal', 1);
        },
    }
}